<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;
use think\facade\Config;
use \think\Db;


/*询问数据*/
class Searchcount extends Base{

	/*列表*/
	public function index(Request $request){
		$this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];

        $data=$this->navleft($data_get);

        $this->assign($data);

	    return $this->fetch();
	}

	public function get_preview_click(Request $request){
		$this->check_session();
	    $get=$request->get();
        $default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
        	$country_where="country!='中国' and ";
        }else{
        	$country_where="";
        }

	    $model=new \app\admin\model\Searchcount();/*调用模型查询*/
        
	    if(empty($get['start'])&&empty($get['end'])){
		 	$model_data=$model->get_preview_click($get['page'],$get['limit'],'0','0');
		 	$start='0';
		 	$end='0';
		}else{
		 	$model_data=$model->get_preview_click($get['page'],$get['limit'],$get['start'],$get['end']);
		 	$start=$get['start'];
		 	$end=$get['end'];
		}

		foreach ($model_data['list'] as $key => $value) {
        	$det='';
        	$BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        	if($start=='0'&&$end=='0'){
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `leftclick`='".$value['leftclick']."' and create_time>='".$BeginDate."'";
        	}else{
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `leftclick`='".$value['leftclick']."' and create_time>='".$start."' and create_time>='".$end."'";
        	}
            $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `leftclick`='".$value['leftclick']."'";
            $detail=Db::connect('publicdata')->query($sql);
            foreach ($detail as $k => $v) {
                $GetBrowser=$this->GetBrowser($v['sys']);
				$Mobile=$this->isMobile($v['sys']);
				if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$GetOs=$this->GetOs($v['sys']);
				//$shebei=$GetBrowser." | ".$isMobile." | ".$GetOs;
				$shebei=$isMobile;
				$det.='{'.$v['ip'].','.$v['country'].','.$shebei.','.$v['create_time'].'}</br>';
            }
            $model_data['list'][$key]['det']=$det;
        }
        
	    $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
	    return json($result);
	}

	public function get_result_click(Request $request){
		$this->check_session();
		$get=$request->get();
		$default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
        	$country_where="country!='中国' and ";
        }else{
        	$country_where="";
        }

		$model=new \app\admin\model\Searchcount();/*调用模型查询*/

        if(empty($get['start'])&&empty($get['end'])){
		 	$model_data=$model->get_result_click($get['page'],$get['limit'],'0','0');
		 	$start='0';
		 	$end='0';
		}else{
		 	$model_data=$model->get_result_click($get['page'],$get['limit'],$get['start'],$get['end']);
		 	$start=$get['start'];
		 	$end=$get['end'];
		}
		foreach ($model_data['list'] as $key => $value) {
        	$det='';
        	$BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        	if($start=='0'&&$end=='0'){
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `rightclick`='".$value['rightclick']."' and create_time>='".$BeginDate."'";
        	}else{
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `rightclick`='".$value['rightclick']."' and create_time>='".$start."' and create_time>='".$end."'";
        	}
            $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `rightclick`='".$value['rightclick']."'";
            $detail=Db::connect('publicdata')->query($sql);
            foreach ($detail as $k => $v) {
                $GetBrowser=$this->GetBrowser($v['sys']);
				$Mobile=$this->isMobile($v['sys']);
				if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$GetOs=$this->GetOs($v['sys']);
				//$shebei=$GetBrowser." | ".$isMobile." | ".$GetOs;
				$shebei=$isMobile;
				$det.='{'.$v['ip'].','.$v['country'].','.$shebei.','.$v['create_time'].'}</br>';
            }
            $model_data['list'][$key]['det']=$det;
        }
        
        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
	}



	public function get_all(Request $request){
		$this->check_session();
		$get=$request->get();
		$default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
        	$country_where="country!='中国' and ";
        }else{
        	$country_where="";
        }
		
		$model=new \app\admin\model\Searchcount();/*调用模型查询*/
	
		if(empty($get['start'])&&empty($get['end'])){
		 	$model_data=$model->get_all($get['page'],$get['limit'],'0','0');
		 	$start='0';
		 	$end='0';
		}else{
		 	$model_data=$model->get_all($get['page'],$get['limit'],$get['start'],$get['end']);
		 	$start=$get['start'];
		 	$end=$get['end'];
		}

        foreach ($model_data['list'] as $key => $value) {
        	$det='';
        	$BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        	if($start=='0'&&$end=='0'){
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `sou`='".$value['sou']."' and create_time>='".$BeginDate."'";
        	}else{
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `sou`='".$value['sou']."' and create_time>='".$start."' and create_time>='".$end."'";
        	}
            $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `sou`='".$value['sou']."'";
            $detail=Db::connect('publicdata')->query($sql);
            foreach ($detail as $k => $v) {
                $GetBrowser=$this->GetBrowser($v['sys']);
				$Mobile=$this->isMobile($v['sys']);
				if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$GetOs=$this->GetOs($v['sys']);
				//$shebei=$GetBrowser." | ".$isMobile." | ".$GetOs;
				$shebei=$isMobile;
				$det.='{'.$v['ip'].','.$v['country'].','.$shebei.','.$v['create_time'].'}</br>';
            }
            $model_data['list'][$key]['det']=$det;
        }
        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
	}

	public function get_all_null(Request $request){
		$this->check_session();
		$get=$request->get();
		$default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
        	$country_where="country!='中国' and ";
        }else{
        	$country_where="";
        }
	
		$model=new \app\admin\model\Searchcount();/*调用模型查询*/

        if(empty($get['start'])&&empty($get['end'])){
		 	$model_data=$model->get_all_null($get['page'],$get['limit'],'0','0');
		 	$start='0';
		 	$end='0';
		}else{
		 	$model_data=$model->get_all_null($get['page'],$get['limit'],$get['start'],$get['end']);
		 	$start=$get['start'];
		 	$end=$get['end'];
		}

		foreach ($model_data['list'] as $key => $value) {
        	$det='';
        	$BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        	if($start=='0'&&$end=='0'){
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `noresult`='".$value['noresult']."' and create_time>='".$BeginDate."'";
        	}else{
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `noresult`='".$value['noresult']."' and create_time>='".$start."' and create_time>='".$end."'";
        	}
            $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `noresult`='".$value['noresult']."'";
            $detail=Db::connect('publicdata')->query($sql);
            foreach ($detail as $k => $v) {
                $GetBrowser=$this->GetBrowser($v['sys']);
				$Mobile=$this->isMobile($v['sys']);
				if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$GetOs=$this->GetOs($v['sys']);
				//$shebei=$GetBrowser." | ".$isMobile." | ".$GetOs;
				$shebei=$isMobile;
				$det.='{'.$v['ip'].','.$v['country'].','.$shebei.','.$v['create_time'].'}</br>';
            }
            $model_data['list'][$key]['det']=$det;
        }
        
        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
	}
    /*导出*/
	public function daochu(Request $request){
		$this->check_session();
		$post=$request->post();
		$default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
        	$country_where="country!='中国' and ";
        }else{
        	$country_where="";
        }

		if(empty($post['start'])&&empty($post['end'])){
		 	$start=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
		 	$end='';
		}else{
			$start=$post['start'];
			$end=$post['end'];
		}
		$model=new \app\admin\model\Searchcount();/*调用模型查询*/
		$model_data=$model->daochu($start,$end,$post['t']);
        if($post['t']=='1'){
        	$ziduan='sou';
        }
        if($post['t']=='2'){
        	$ziduan='noresult';
        }
        if($post['t']=='3'){
        	$ziduan='leftclick';
        }
        if($post['t']=='4'){
        	$ziduan='rightclick';
        }
		foreach ($model_data as $key => $value) {
        	$det='';
        	$BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        	if($start=='0'&&$end=='0'){
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `".$ziduan."`='".$value[$ziduan]."' and create_time>='".$BeginDate."'";
        	}else{
               $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `".$ziduan."`='".$value[$ziduan]."' and create_time>='".$start."' and create_time>='".$end."'";
        	}
            $sql="SELECT ip,country,sys,create_time FROM lily_searchcount where ".$country_where." `".$ziduan."`='".$value[$ziduan]."'";
            $detail=Db::connect('publicdata')->query($sql);
            foreach ($detail as $k => $v) {
                $GetBrowser=$this->GetBrowser($v['sys']);
				$Mobile=$this->isMobile($v['sys']);
				if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$GetOs=$this->GetOs($v['sys']);
				//$shebei=$GetBrowser." | ".$isMobile." | ".$GetOs;
				$shebei=$isMobile;
				$det.='{'.$v['ip'].';'.$v['country'].';'.$shebei.';'.$v['create_time'].'} , ';
            }
            $model_data[$key]['det']=$det;
        }

		$data=[];
		foreach ($model_data as $key => $value) {
			foreach ($value as $k => $v) {
				$data[$key][]=$v;
			}
		}
		$result=["status"=>1,"data"=>$data];
        return json($result);
		
	}


	public function  all_search(Request $request){
		$this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];

        $data=$this->navleft($data_get);
        $Searchcount=new \app\admin\model\Searchcount();/*调用模型查询*/
		$Searchcount_data=$Searchcount->all_search();
		
		foreach ($Searchcount_data as $key => $value) {
			$GetBrowser=$this->GetBrowser($value['sys']);
			$Mobile=$this->isMobile($value['sys']);
			if($Mobile){
				$isMobile='phone';
			}else{
				$isMobile='pc';
			}
			$GetOs=$this->GetOs($value['sys']);
			$Searchcount_data[$key]['shebei']=$GetBrowser." | ".$isMobile." | ".$GetOs;
			if(!empty($value['sou'])){
				$Searchcount_data[$key]['status']='回车';
			}
			if(!empty($value['leftclick'])){
				$Searchcount_data[$key]['status']='左侧点击';
			}
			if(!empty($value['rightclick'])){
				$Searchcount_data[$key]['status']='右侧点击';
			}
			if(!empty($value['noresult'])){
				$Searchcount_data[$key]['status']='无结果';
			}
		}
		
        $data['all']=$Searchcount_data;

        $this->assign($data);
        
	    return $this->fetch();
	}



    public function GetBrowser($anget){
	    if(!empty($anget))
	    {
		    $br = $anget;
		    if (preg_match('/MSIE/i',$br)){
		       $br = 'MSIE';
		    }elseif (preg_match('/Firefox/i',$br)){
		       $br = 'Firefox';
		    }elseif (preg_match('/Chrome/i',$br)){
		       $br = 'Chrome';
		    }elseif (preg_match('/Safari/i',$br)){
		       $br = 'Safari';
		    }elseif (preg_match('/Opera/i',$br)){
		       $br = 'Opera';
		    }else {
		       $br = 'Other';
		    }
	        return $br;
	    }else{
	        return "未知浏览器";
	    }
	}

    public function isMobile($anget) {
		static $is_mobile = null; 
		if ( isset( $is_mobile ) ) {
			return $is_mobile;
		} 
		if ( empty($anget) ) {
				$is_mobile = false;
		} elseif ( strpos($anget, 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
				|| strpos($anget, 'Android') !== false
				|| strpos($anget, 'Silk/') !== false
				|| strpos($anget, 'Kindle') !== false
				|| strpos($anget, 'BlackBerry') !== false
				|| strpos($anget, 'Opera Mini') !== false
				|| strpos($anget, 'Opera Mobi') !== false ) {
				$is_mobile = true;
		} else {
			$is_mobile = false;
		} 
		return $is_mobile;
    }


    public function GetOs($ang){
    	$agent=$ang;
		$os = false;
		if (preg_match('/win/i', $agent) && strpos($agent, '95'))
		{
		$os = 'Windows 95';
		}
		else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
		{
		$os = 'Windows ME';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
		{
		$os = 'Windows 98';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
		{
		$os = 'Windows Vista';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
		{
		$os = 'Windows 7';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
		{
		$os = 'Windows 8';
		}else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
		{
		$os = 'Windows 10';#添加win10判断
		}else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
		{
		$os = 'Windows XP';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
		{
		$os = 'Windows 2000';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
		{
		$os = 'Windows NT';
		}
		else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
		{
		$os = 'Windows 32';
		}
		else if (preg_match('/linux/i', $agent))
		{
		$os = 'Linux';
		}
		else if (preg_match('/unix/i', $agent))
		{
		$os = 'Unix';
		}
		else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
		{
		$os = 'SunOS';
		}
		else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
		{
		$os = 'IBM OS/2';
		}
		else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
		{
		$os = 'Macintosh';
		}
		else if (preg_match('/PowerPC/i', $agent))
		{
		$os = 'PowerPC';
		}
		else if (preg_match('/AIX/i', $agent))
		{
		$os = 'AIX';
		}
		else if (preg_match('/HPUX/i', $agent))
		{
		$os = 'HPUX';
		}
		else if (preg_match('/NetBSD/i', $agent))
		{
		$os = 'NetBSD';
		}
		else if (preg_match('/BSD/i', $agent))
		{
		$os = 'BSD';
		}
		else if (preg_match('/OSF1/i', $agent))
		{
		$os = 'OSF1';
		}
		else if (preg_match('/IRIX/i', $agent))
		{
		$os = 'IRIX';
		}
		else if (preg_match('/FreeBSD/i', $agent))
		{
		$os = 'FreeBSD';
		}
		else if (preg_match('/teleport/i', $agent))
		{
		$os = 'teleport';
		}
		else if (preg_match('/flashget/i', $agent))
		{
		$os = 'flashget';
		}
		else if (preg_match('/webzip/i', $agent))
		{
		$os = 'webzip';
		}
		else if (preg_match('/offline/i', $agent))
		{
		$os = 'offline';
		}
		else
		{
		$os = '未知操作系统';
		}
		return $os;
	}


}

